"""
变量模型
"""
from datetime import datetime
from app.extensions import db


class Variable(db.Model):
    """变量模型"""
    __tablename__ = 'variable'
    
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)  # 变量名称
    key = db.Column(db.String(100), unique=True, nullable=False)  # 变量键名（唯一）
    value = db.Column(db.Text, nullable=False)  # 变量值
    category = db.Column(db.String(50), nullable=False)  # 变量分类
    description = db.Column(db.Text)  # 变量描述
    is_active = db.Column(db.Boolean, default=True)  # 是否启用
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'name': self.name,
            'key': self.key,
            'value': self.value,
            'category': self.category,
            'description': self.description,
            'is_active': self.is_active,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S'),
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S')
        }